package com.kl.forum.task;

import com.kl.forum.dao.ArticleMapper;
import com.kl.forum.dao.CircleArticleMapper;
import com.kl.forum.entity.Article;
import com.kl.forum.entity.CircleArticle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ScheduledArticleTask   {

    @Autowired
    private CircleArticleMapper circleArticleMapper;
    @Autowired
    private ArticleMapper articleMapper;

    @Scheduled(fixedDelay = 60000)
    public String findArticle(){
        Map map=new HashMap();
//        1代表未发送
        map.put("pushFlag",1);
        try {
            List<Article> list= articleMapper.findArticleByPushFlag(map);
            if(list.size()>0&&list!=null) {
                for (int i = 0; i < list.size(); i++) {
                    long time = new Date().getTime();
                    if (list.get(i).getPushTime().getTime() <= time) {
                        list.get(i).setPushFlag(0);
                        list.get(i).setIsDel(1);
                        articleMapper.updateByPrimaryKeySelective(list.get(i));
                    }
                }
            }
            List<CircleArticle> list1= circleArticleMapper.findArticleByPushFlag(map);
            if(list1.size()>0&&list1!=null) {
                for (int i = 0; i < list1.size(); i++) {
                    long time = new Date().getTime();
                    if (list1.get(i).getPushTime().getTime() <= time) {
                        list1.get(i).setPushFlag(0);
                        list1.get(i).setIsDel(1);
                        circleArticleMapper.updateByPrimaryKeySelective(list1.get(i));
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return "定时发帖成功";

    }

}
